package com.tjzx.struts.reserve;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;

import com.tjzx.common.Utils;
import com.tjzx.entity.EipReserveMedical;
import com.tjzx.service.EipReserveMedicalService;
public class ReserveMedicalAction extends ActionSupport{
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		WebApplicationContext ctx = this.getWebApplicationContext();
		EipReserveMedicalService ts = (EipReserveMedicalService)ctx.getBean("eipReserveMedicalService");
		String method = request.getParameter("method");
		if (Utils.isEmpty(method)) {
			return mapping.findForward("error");
		} else if (method.equals("add")){
		   executeAdd(mapping, form, request, response, ts);
		   return mapping.findForward("success");
		} else if (method.equals("search")){
			request.setAttribute("searchdo","input");
			return mapping.findForward("success");
		}else if (method.equals("showsearch")){
			request.setAttribute("searchdo", "findrs");
			executeShow(mapping, form, request, response, ts);
			return mapping.findForward("success");
		}
		return mapping.findForward("error");
	}
	public void executeAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipReserveMedicalService ts) {
		String userName = request.getParameter("UserName");
		String IdentifyNo = request.getParameter("IdentifyNo");
		String ReserveType = request.getParameter("ReserveType");
		String BigType = request.getParameter("BigType");
		String smallType = request.getParameter("smallType");
		String ReserveTime = request.getParameter("ReserveTime");
		String tel = request.getParameter("tel");
		String unit = request.getParameter("unit");
		String adress = request.getParameter("adress");
		String content = request.getParameter("content");
		
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		try {
			date = sdf.parse(ReserveTime);
		}catch (ParseException pe) {
            System.out.println(pe.getMessage());
        }

		Timestamp time=new Timestamp(date.getTime());

		EipReserveMedical reserve =new EipReserveMedical();
		reserve.setReserveName(userName);
		//reserve.setIdentifyNo(IdentifyNo); TODO update database table
		reserve.setReserveType(ReserveType);
		reserve.setBigType(BigType);
		reserve.setSmallType(smallType);
		reserve.setReserveTime(time);
		reserve.setReserveUnit(unit);
		reserve.setReserveTel(tel);
		reserve.setReserveAdress(adress);
		reserve.setReserveInfo(content);
		ts.addReserveMedical(reserve);
	}
	public void executeShow(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipReserveMedicalService ts) {
		String username = request.getParameter("username");
		String tel = request.getParameter("tel");
		List<EipReserveMedical> reserveList = ts.getEipReserveMedical(username, tel);
		if(reserveList.size() == 0){
			request.setAttribute("rsList", 0);
		}
		request.setAttribute("reserveList", reserveList);
	}
}
